package palmtop.com.palmtop.thread;


import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;


import net.grandcentrix.tray.AppPreferences;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;

import java.text.SimpleDateFormat;
import java.util.Date;

import palmtop.com.palmtop.util.Config;
import palmtop.com.palmtop.util.NetUtil;

/**
 * Created by Administrator on 2018/8/24.
 */

public final class NotificationThread extends Thread {

    public static  boolean exit = false;
    Context context ;
    String ssgrid;
    String username;
    String userid;
    String role_id;
    MediaPlayer mMediaPlayer = new MediaPlayer();
    public NotificationThread(Context c)
    {
        this.context = c;
        AppPreferences appPreferences = new AppPreferences(context);
        userid = appPreferences.getString("userid", "");
        username = appPreferences.getString("name", "");
        ssgrid =appPreferences.getString("ssgrid", "");
        role_id = appPreferences.getString("role_id", "");
    }

    @Override
    public  void run() {

            while(!exit){
                try {
                    selectNoByUserid();
                    Thread.sleep(10000);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            if(exit==true){
                exit = false;
            }




    }

    private void selectNoByUserid() {
        RequestParams params = new RequestParams(Config.SERVER_URL + "taskapp/selectNoByUserid");

        //73006b03b7f447bf953d065462e5b1ac
        if(role_id.equals("73006b03b7f447bf953d065462e5b1ac")){
            params.addBodyParameter("userid", "73006b03b7f447bf953d065462e5b1ac");
        }
        if(role_id.equals("d18ac0ef29ee477f8e3dd214245a1de5")){
            params.addBodyParameter("userid", ssgrid);
        }
        if(role_id.equals("1f515845ed214961aef8a0bc6646cf2d")){
            params.addBodyParameter("userid", userid);
        }
        NetUtil.postData(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if(result.equals("having")){
                    try{
                        mMediaPlayer.setDataSource(Config.base_url+"/palmtop/notification/notification.mp3");
                        mMediaPlayer.prepare();
                        mMediaPlayer.start();
                        updateNoByUserid();
                    }catch (Exception ex){}

                }else{
                }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {
            }
        });

    }

    private void updateNoByUserid(){
        RequestParams params = new RequestParams(Config.SERVER_URL + "taskapp/updateNoByUserid");
        if(role_id.equals("73006b03b7f447bf953d065462e5b1ac")){
            params.addBodyParameter("userid", "73006b03b7f447bf953d065462e5b1ac");
        }
        if(role_id.equals("d18ac0ef29ee477f8e3dd214245a1de5")){
            params.addBodyParameter("userid", ssgrid);
        }
        if(role_id.equals("1f515845ed214961aef8a0bc6646cf2d")){
            params.addBodyParameter("userid", userid);
        }
        NetUtil.postData(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {
            }
        });
    }


}
